package com.xbongbong.saas.util;

import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;

import java.util.List;
import java.util.Objects;

/**
 *  Fact类型为JSONObject时，比较表达式 测试
 * 适用于规则存储时，即保存为Spel表达式格式(此操作由调用方完成)
 * @author zheng.li
 **/
public class RuleEngineSpelTransform {

    /**
     * 字段值
     */
    private List<Object> conditionValueList;

    /**
     * 字段名
     */
    private String conditionAttr;

    /**
     * 字段名
     */
    private Integer fieldType;

    /**
     * 条件枚举
     */
    private ConditionEnum conditionEnum;

    /**
     * 单值
     */
    private String conditionValue;



    public RuleEngineSpelTransform(String conditionAttr, List<Object> conditionValueList, ConditionEnum conditionEnum,Integer fieldType) {
        this.conditionAttr = conditionAttr;
        this.conditionValueList = conditionValueList;
        this.conditionEnum = conditionEnum;
        this.fieldType = fieldType;

        this.conditionValue  = CollectionsUtil.isNotEmpty(conditionValueList) ? conditionValueList.get(0).toString() : "";
    }


    public String operation() {
        switch (conditionEnum) {
            case EQUAL:
                return equal();
            case NOEQUAL:
                return notEqual();
            case EMPTY:
                return empty();
            case NOEMPTY:
                return notEmpty();
            case ALLINCLUDE:
                return allInclude();
            case INCLUDE:
                return include();
            case IN:
                return in();
            case NOIN:
                return noIn();
            case LIKE:
                return like();
            case NOLIKE:
                return noLike();
            case LESSTHAN:
                return lessThan();
            case LESSEQUAL:
                return lessEqual();
            case GREATERTHAN:
                return greaterThan();
            case GREATEREQUAL:
                return greaterEqual();
            case RANGE:
                return range();
            default:
                break;
        }
        return "";
    }

    /**
     * 相等
     * @return String
     */
    public String equal() {
        if (conditionValue == null) {
            return "false";
        }
        if (Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            return "T(java.util.Objects).equals(\"" +conditionValueList + "\",#JSONObject.get(\"" + conditionAttr + "\")?.toString())";
        } else if (Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.CHECKBOX_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())) {
            // TODO 存疑问？多选的条件 等于 【1，2，3】 中的3
            //  那么 value= 【2，3】这种情况算吗
                return "#JSONObject.get(\"" + conditionAttr +"\")?.contains(\"" + conditionValue + "\")";
        } else {
            return "T(java.util.Objects).equals(\"" +conditionValue + "\",#JSONObject.get(\"" + conditionAttr + "\"))";
        }
    }


    /**
     * 不等
     * @return String
     */
    public String notEqual() {
        if (conditionValue == null) {
            return "false";
        }
        return "!T(java.util.Objects).equals(\"" +conditionValue + "\",#JSONObject.get(\"" + conditionAttr + "\")?.toString())";
    }



    /***
     * 为空
     * @return  String
     */
    public String empty() {
        String ret = "(#JSONObject.get(\""+ conditionAttr + "\") == null || #JSONObject.getString(\"" + conditionAttr + "\").isEmpty())";
        if (Objects.equals(fieldType, FieldTypeEnum.CHECKBOX_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            return "(" + ret + " || " + listEmpty() + ")";
        }
        return ret;
    }

    /**
     * 不为空
     * @return String
     */
    public String notEmpty() {
        String ret = "(#JSONObject.get(\""+ conditionAttr + "\") != null && !#JSONObject.getString(\"" + conditionAttr + "\").isEmpty())";
        if (Objects.equals(fieldType, FieldTypeEnum.COMBO_CHECK.getType())
                || Objects.equals(fieldType, FieldTypeEnum.USER_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.DEPT_GROUP.getType())
                || Objects.equals(fieldType, FieldTypeEnum.LABEL.getType())) {
            return "(" + ret + " && !" + listEmpty() + ")";
        }
        return ret;
    }


    private String listEmpty() {
        return "\"[]\".equals(#JSONObject.get(\"" + conditionAttr + "\"))";
    }


    /**
     * 大于
     * @return String
     */
    public String greaterThan() {
        return "#JSONObject.get(\"" + conditionAttr +"\") > " + conditionValue;
    }

    /**
     * 大于等于
     * @return String
     */
    public String greaterEqual() {
        return "#JSONObject.get(\"" + conditionAttr +"\") >= " + conditionValue;
    }

    /**
     * 小于
     * @return String
     */
    public String lessThan() {
        return "#JSONObject.get(\"" + conditionAttr +"\") < " + conditionValue;
    }

    /**
     * 小于等于
     * @return String
     */
    public String lessEqual() {
        return "#JSONObject.get(\"" + conditionAttr +"\") <= " + conditionValue;
    }

    /**
     * 同时包含
     * @return String
     */
    public String allInclude() {
        String listString = listString(conditionValueList);
        return "#JSONObject.get(\""  + conditionAttr +"\")"  + "?.containsAll(" + listString + ")";
    }

    /**
     * 包含任意一个
     * @return String
     */
    public String include() {
        String listString = listString(conditionValueList);
        String ret =  "!#JSONObject.getString(\"" + conditionAttr +"\")?.isEmpty() && ";
        StringBuilder sb = new StringBuilder(ret);
        for (int i = 0;i< conditionValueList.size(); i++) {
            if (i > 0) {
                sb.append(" || #JSONObject.get(\"").append(conditionAttr).append("\").contains(").append(listString).append("[").append(i).append("])");
            } else {
                sb.append("(#JSONObject.get(\"").append(conditionAttr).append("\").contains(").append(listString).append("[0])");
            }
        }
        sb.append(")");
        return sb.toString();
    }

    /**
     * 包含
     * @return String
     */
    public String like() {
        return "#JSONObject.getString(\"" + conditionAttr +"\")?.contains(\"" + conditionValue + "\")";
    }

    /**
     * 不包含
     * @return String
     */
    public String noLike() {
        return "!" + like();
    }

    /**
     * 等于任意一个
     * @return String
     */
    public String in() {

        if (conditionValueList.isEmpty()) {
            return "false";
        }
        String listString = listString(conditionValueList);
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i< conditionValueList.size(); i++) {
            if (i > 0) {
                sb.append(" || T(java.util.Objects).equals(#JSONObject.get(\"").append(conditionAttr).append("\"),").append(listString).append("[").append(i).append("])");
            } else {
                sb.append("( T(java.util.Objects).equals(#JSONObject.get(\"").append(conditionAttr).append("\"),").append(listString).append("[0])");
            }
        }
        sb.append(")");
        return sb.toString();
        //return "@ruleEngineUtil.inSpel(#JSONObject.get('" + conditionAttr + "')," + listString + ")";
    }

    private String listString(List list) {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        for (int i = 0; i< list.size(); i++) {
            Object o = list.get(i);
            if (o instanceof String && !Objects.equals(fieldType,FieldTypeEnum.NUM.getType())) {
                // 要转不然是纯数字的字符串可能会报错
                o = "\"" + o + "\"";
            }
            if (i > 0) {
                sb.append(",").append(o);
            } else {
                sb.append(o);
            }

        }
        sb.append("}");
        return sb.toString();
    }

    /**
     * 不等于任意一个
     * @return String
     */
    public String noIn() {
        if (conditionValueList.isEmpty()) {
            return "false";
        }
        String listString = listString(conditionValueList);
        StringBuilder sb = new StringBuilder();
        for (int i = 0;i< conditionValueList.size(); i++) {
            if (i > 0) {
                sb.append(" || !T(java.util.Objects).equals(#JSONObject.get(\"").append(conditionAttr).append("\"),").append(listString).append("[").append(i).append("])");
            } else {
                sb.append("( !T(java.util.Objects).equals(#JSONObject.get(\"").append(conditionAttr).append("\"),").append(listString).append("[0])");
            }
        }
        sb.append(")");
        return sb.toString();
    }

    /**
     * 大于等于某个值，并且小于某个值
     * @return String
     */
    public String range() {
        if (conditionValueList.size() < 2) {
            return "false";
        }

        return "#JSONObject.get(\"" + conditionAttr +"\") >= " + listString(conditionValueList) + "[0]"
                + "&& #JSONObject.get(\"" + conditionAttr + "\") < " + listString(conditionValueList) + "[1]";
    }



}
